<?php
class TestCommand extends BaseCommand
{
    public function actionDeleteUser($userId)
    {
        /******************MYSQL******************/
        // disable check constraint
        Yii::app()->db->createCommand('SET FOREIGN_KEY_CHECKS = 0')->execute();

        // delete photos
        $criteria = new CDbCriteria();
        $criteria->condition = 'user_id = :userId';
        $criteria->params = array(':userId' => $userId);
        $photos = PhotoModel::model()->findAll($criteria);
        $thumbnails = ThumbnailModel::model()->findAll();
        foreach($photos as $photo)
        {
            foreach($thumbnails as $thumbnail)
            {
                @unlink(ThumbnailUtility::getPath($thumbnail->width, $thumbnail->height).'/'.$photo->filename);
            }
            @unlink(PhotoUtility::getPath().'/'.$photo->filename);
            $photo->delete();
        }
        
        // delete user
        UserModel::model()->deleteByPk($userId);

        // delete friends
        FriendModel::model()->deleteAllByAttributes(array('user_id' => $userId));
        
        // delete user_work
        $sql = 'DELETE FROM {{user_work}} WHERE user_id = :userId';
        Yii::app()->db->createCommand($sql)
                      ->bindParam(':userId', $userId)
                      ->execute();
        
        // delete user_education
        $sql = 'DELETE FROM {{user_education}} WHERE user_id = :userId';
        Yii::app()->db->createCommand($sql)
                      ->bindParam(':userId', $userId)
                      ->execute();

        // enable check constraint
        Yii::app()->db->createCommand('SET FOREIGN_KEY_CHECKS = 1')->execute();
        /******************MYSQL******************/

        /******************REDIS******************/
        // friend list
        $friendlist = new FriendListKey($userId);
        $friendlist->delete();

        // snapshot
        $snapshot = new SnapshotKey($userId);
        $snapshot->delete();
        /******************REDIS******************/
    }

    public function actionCreateUsers($fromId, $toId)
    {
        for($userId = $fromId; $userId <= $toId; $userId++)
        {
            if(UserUtility::getById($userId))
            {
                continue;
            }
            UserUtility::add($userId);
            UserUtility::daily($userId);
            UserUtility::login($userId);
        }
    }

    public function actionFlush()
    {
        // mysql
        $tables = Yii::app()->db->createCommand('SHOW TABLES')->queryColumn();
        $sql = 'SET FOREIGN_KEY_CHECKS = 0; DROP TABLE %s; SET FOREIGN_KEY_CHECKS = 1;';
        $sql = sprintf($sql, implode(',', $tables));
        Yii::app()->db->createCommand($sql)->execute();
        echo 'FLUSH MYSQL DONE!', "\n";

        // redis
        Yii::app()->redis->getClient()->flushDB();
        echo 'FLUSH REDIS DONE!', "\n";

        // photo
        if(strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')
        {
            $photoPath = Yii::app()->params[ENUM_CONFIG_PARAM_PHOTO_PATH];
            $cmd = sprintf('find %s -name "*.jpg" -delete', $photoPath);
            exec($cmd);
            echo 'DELETE ALL PHOTOS DONE!', "\n";
        }

        // migrate
        if(strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')
        {
            $basePath = Yii::app()->getBasePath();
            $cmd = sprintf('php %s/yiic migrate --interactive=0', $basePath);
            exec($cmd);
            echo 'MIGRATE DONE!', "\n";
        }
    }

    public function actionClearQueue()
    {
        Yii::app()->redis->del('gearman_queue');
    }

    public function actionTest()
    {
        $key = new ARedisList('huynh_list12');
        $key->delete();
        $key = new ARedisList('huynh_list12');
        //var_dump($key->pushMulti(1,2,3,4,5));
        var_dump(Yii::app()->redis->getClient()->lpush('huynh_list12', 1,2,3,4,5));
    }
}